package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.util.DateUtil;
import com.coding.dao.QuestionDao;
import com.coding.dao.UserDao;
import com.coding.dao.WrongQuestionDao;
import com.coding.pojo.Question;
import com.coding.pojo.User;
import com.coding.pojo.WrongQuestion;
import com.coding.service.WrongQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auther wangchenghao
 * @date 2020/10/3
 **/
@Service
public class WrongQuestionImpl implements WrongQuestionService {
    @Autowired
    WrongQuestionDao wrongQuestionDao;
    @Autowired
    QuestionDao questionDao;
    @Autowired
    UserDao userDao;

    @Override
    public void selectById(int question_id, int user_id, BaseModel model) throws Exception {
        Question question = questionDao.selectByPrimaryKey(question_id);
        if (question == null) {
            model.setFailure("查无此题");
            return;
        }
        User user = userDao.selectById(user_id);
        if (user == null) {
            model.setFailure("查无此用户");
            return;
        }

        WrongQuestion wrongQuestion = wrongQuestionDao.selectByPrimaryKey(question_id, user_id);
        if (wrongQuestion != null) {
            model.setSucceed("查询到该错题", wrongQuestion);
        } else {
            model.setFailure("我查询到改错题");
        }
    }

    @Override
    public void selectByUser(int user_id, BaseModel model) throws Exception {
        User user = userDao.selectById(user_id);
        if (user == null) {
            model.setFailure("查无此用户");
            return;
        }
        List<WrongQuestion> wrongQuestionList = wrongQuestionDao.selectByUser(user_id);
        if (wrongQuestionList.size() == 0) {
            model.setFailure("该用户无错题");
        } else {
            model.setSucceed("查询至该用户错题", wrongQuestionList);
        }
    }

    @Override
    public void insertWrong(WrongQuestion wrongQuestion, BaseModel model) throws Exception {
        User user = userDao.selectById(wrongQuestion.getUser_id());
        if (user == null) {
            model.setFailure("查无此用户");
            return;
        }
        Question question = questionDao.selectByPrimaryKey(wrongQuestion.getQuestion_id());
        if (question == null) {
            model.setFailure("查无此题");
            return;
        }
        WrongQuestion wrong=wrongQuestionDao.selectByPrimaryKey(wrongQuestion.getQuestion_id(),wrongQuestion.getUser_id());
        if (wrong!=null){
            model.setFailure("已添加此题",wrong);
            return;
        }
        wrongQuestion.setTrue_choose(question.getAnswer());
        wrongQuestion.setTrue_content(question.getAnswer_content());
        wrongQuestion.setWrong_time(DateUtil.getStringDate());
        if (wrongQuestionDao.insertSelective(wrongQuestion) == 1) {
            model.setSucceed("新增错题成功", wrongQuestion);
        } else {
            model.setFailure("新增错题失败");
        }
    }

    @Override
    public void deleteWrong(int question_id, int user_id, BaseModel model) {
        Question question = questionDao.selectByPrimaryKey(question_id);
        if (question == null) {
            model.setFailure("查无此题");
            return;
        }
        User user = userDao.selectById(user_id);
        if (user == null) {
            model.setFailure("查无此用户");
            return;
        }
        WrongQuestion wrongQuestion = wrongQuestionDao.selectByPrimaryKey(question_id, user_id);
        if (wrongQuestion == null) {
            model.setFailure("用户没有改错题");
            return;
        }
        if (wrongQuestionDao.deleteByPrimaryKey(question_id, user_id) == 1) {
            model.setSucceed("用户删除成功");
        } else {
            model.setFailure("用户删除诗词失败");
        }
    }
}
